﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//请实现，在Game窗口长按鼠标用LineRender画出鼠标移动的轨迹
public class Lesson21_code2 : MonoBehaviour
{
    LineRenderer line;
    // Start is called before the first frame update
    void Start()
    {
        //GameObject obj = new GameObject();
        //obj.name = "R";
        //line = obj.AddComponent<LineRenderer>();
        //line.loop = false;
        //line.startWidth = 0.5f;
        //line.endWidth = 0.5f;
    }

    // Update is called once per frame
    Vector3 nowPos;
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            GameObject obj = new GameObject();
            obj.name = "R";
            line = obj.AddComponent<LineRenderer>();
            line.loop = false;
            line.startWidth = 0.5f;
            line.endWidth = 0.5f;
            line.positionCount = 0;
        }
        if (Input.GetMouseButton(0))
        {
            line.positionCount += 1;
            //如何得到鼠标转世界坐标的 对应点
            //知识点
            //1.得到鼠标位置：Input.mousePosition
            //2.怎么把鼠标转世界坐标
            //Camera.main.ScreenToWorldPoint(nowPos)
            nowPos = Input.mousePosition;
            //得到的屏幕坐标Z轴是0，所以转世界坐标需要将其往前推，否则看不到
            nowPos.z = 10;
            line.SetPosition(line.positionCount-1, Camera.main.ScreenToWorldPoint(nowPos));
        }
    }
}
